package com.foxzzz.binary.search;


public class Solution50 {
    public double myPow(double x, int n) {
        x = n > 0 ? x : 1 / x;
        n = Math.abs(n);
        return pow(x, n);
    }

    public double pow(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        double half = pow(x, n / 2);
        if (n % 2 == 0) {
            return half * half;
        } else {
            return half * half * x;
        }
    }

    //右移会报错！！！！具体原因。。
    public double pow1(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        double half = pow1(x, n >> 1);
        if (n % 2 == 0) {
            return half * half;
        } else {
            return half * half * x;
        }
    }
    public static void main(String[] args) {

        System.out.println(new Solution50().myPow(2.00000, 10));
        System.out.println(new Solution50().myPow(2.00000, 8));
        System.out.println(new Solution50().myPow(8.84372, -5));
        System.out.println(new Solution50().myPow(2.00000, -2147483648));

    }
}
